package cc.chenwenxi.mypi.appium.kits;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidElement;
import org.openqa.selenium.NotFoundException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Sleeper;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Clock;
import java.util.concurrent.TimeUnit;


public class AppiumDriverWait extends FluentWait<AppiumDriver> {
    //默认轮询时间(毫秒)
    public final static long DEFAULT_POLLINGEVERY_TIMEMILLS = 500;
    public final static long DEFAULT_TIMEOUT_INSECONDS = 500l;

    /**
     * 重写的等待
     * @param driver
     * @param element
     * @return
     */
    public WebElement untilMy(AppiumDriver driver, AndroidElement element){
        return  new AppiumDriverWait(driver,60).until(new  ExpectedConditionForAppium<WebElement>(){

            @Override
            public WebElement apply(AppiumDriver driver) {
                AndroidElement visibleElement = (AndroidElement) ExpectedConditions.visibilityOf(element).apply(driver);

                try {
                    return visibleElement != null && visibleElement.isEnabled() ? visibleElement : null;
                } catch (StaleElementReferenceException var4) {
                    return null;
                }
            }

            @Override
            public String toString() {
                return "element to be 点击: " + element;
            }
        });
    }

    /**
     * Wait will ignore instances of NotFoundException that are encountered (thrown) by default in
     * the 'until' condition, and immediately propagate all others.  You can add more to the ignore
     * list by calling ignoring(exceptions to add).
     *
     * @param driver The AppiumDriver instance to pass to the expected conditions
     * @see WebDriverWait#ignoring(java.lang.Class)
     */
    public AppiumDriverWait(AppiumDriver driver) {
        this(driver, Clock.systemDefaultZone(), Sleeper.SYSTEM_SLEEPER, DEFAULT_TIMEOUT_INSECONDS, DEFAULT_POLLINGEVERY_TIMEMILLS);
    }

    /**
     * Wait will ignore instances of NotFoundException that are encountered (thrown) by default in
     * the 'until' condition, and immediately propagate all others.  You can add more to the ignore
     * list by calling ignoring(exceptions to add).
     *
     * @param driver           The AppiumDriver instance to pass to the expected conditions
     * @param timeOutInSeconds The timeout in seconds when an expectation is called
     * @see WebDriverWait#ignoring(java.lang.Class)
     */
    public AppiumDriverWait(AppiumDriver driver, long timeOutInSeconds) {
        this(driver, Clock.systemDefaultZone(), Sleeper.SYSTEM_SLEEPER, timeOutInSeconds, DEFAULT_POLLINGEVERY_TIMEMILLS);
    }

    /**
     * Wait will ignore instances of NotFoundException that are encountered (thrown) by default in
     * the 'until' condition, and immediately propagate all others.  You can add more to the ignore
     * list by calling ignoring(exceptions to add).
     *
     * @param driver           The WebDriver instance to pass to the expected conditions
     * @param timeOutInSeconds The timeout in seconds when an expectation is called
     * @param sleepInMillis    The duration in milliseconds to sleep between polls.
     * @see WebDriverWait#ignoring(java.lang.Class)
     */
    public AppiumDriverWait(AppiumDriver driver, long timeOutInSeconds, long sleepInMillis) {
        this(driver, Clock.systemDefaultZone(), Sleeper.SYSTEM_SLEEPER, timeOutInSeconds, sleepInMillis);
    }

    protected AppiumDriverWait(AppiumDriver driver, Clock clock, Sleeper sleeper, long timeOutInSeconds,
                               long sleepTimeOut) {
        super(driver, clock, sleeper);
        withTimeout(timeOutInSeconds, TimeUnit.SECONDS);
        pollingEvery(sleepTimeOut, TimeUnit.MILLISECONDS);
        ignoring(NotFoundException.class);
    }


}